import http.*
import korlibs.korge.scene.*
import korlibs.korge.service.storage.*
import korlibs.korge.view.*
import korlibs.render.*

class LoginScene : Scene() {
    override suspend fun SContainer.sceneMain() {
        while (storage.getOrNull("id") == null) {
            login()
            if (storage.getOrNull("id") == null) {
                views.gameWindow.alert("Не удалось войти. Попробуйте еще раз")
            }
        }
        sceneContainer.changeTo { HousesScene() }
    }

    private suspend fun login() {
        val httpClient = PointsHttpClient(storage["serverIp"], "")
        val name = views.gameWindow.prompt("Представьтесь, профессор")
        httpClient.login(name)?.let {
            storage["id"] = it
        }
    }
}
